package routers

import (
	"gin_template/core/logger"
	"gin_template/core/middleware"
	"gin_template/routers/subRouters"
	"net/http"

	gs "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"

	_ "gin_template/docs"

	"github.com/gin-gonic/gin"
)

// InitRouter initialize routing information
func InitRouter() *gin.Engine {

	router := gin.Default()
	// jwt 签权验证
	// router.Use(middleware.JWT())
	// log日志
	router.Use(middleware.GinLogger(logger.Logger), middleware.GinRecovery(logger.Logger, true))
	// // 捕获全局错误处理
	// router.Use(middleware.HandleErrors())

	router.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))

	router.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{
			"status":  404,
			"message": "找不到该路由",
		})
		return
	})

	router.NoMethod(func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{
			"status":  404,
			"message": "找不到该方法",
		})
		return
	})

	// 路由注册
	subRouters.RegisterApiRouter(router)
	return router
}
